<?php
/**
 * 后台管理员管理
 *
 * 后台管理员管理controller
 *
 * @author    zhouchao <zhouchao@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
namespace App\Http\Controllers\Backstage;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
use App\Role;
use Hash;
use DB;

class AdministratorsController extends Controller
{
    public function __construct()
    {
    }

    /**
     * 获取管理员列表
     * @return $this
     */
    public function getList(){
        //获取管理员数据
        //$admins = DB::table('admins')->where('state',1)->get();
        $admins = User::all();
        return view('backstage.admin.list')->with('admins',$admins);
    }

    /**
     * 添加管理员界面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function getAdd(){
        return view('backstage.admin.add');
    }

    /**
     * 添加管理员
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function postAdd(Request $request){
        $this->validate($request,[
            'username'=>'required|max:32|unique:admins',
            'email' => 'required|email|unique:admins',
            'password'=>'required|min:6'
        ]);
        $username = trim($request->input('username'));
        $email = $request->input('email');
        $password = trim($request->input('password'));
        $admin = new User;
        $admin->username = $username;
        $admin->email = $email;
        $admin->password = Hash::make($password);
        $admin->created_at = date('Y-m-d H:i:s',time());
        $admin->updated_at = date('Y-m-d H:i:s',time());
        $admin->save();
        return empty($admin)?redirect()->back()->with('error_message', '添加失败') :
            redirect('backstage/administrators/list')->with('success_message', '添加成功');
    }

    /**
     * 删除管理员
     * @param $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function getDelete($id){
        $admin = User::find(intval($id));
        if($admin->is_super_administrator == 1){
            return redirect()->back()->with('error_message', '超级管理员不能删除');
        }
        User::where('id', intval($id))->delete();
        //删除用户
        DB::table('role_user')->where('user_id','=',intval($id))->delete();
        return redirect()->back()->with('success_message', '删除成功');
    }

    /**
     * 更新页面
     * @param $id
     * @return $this
     */
    public function getUpdate($id){
        $admin = User::find(intval($id));
        return view('backstage.admin.edit')
            ->with('admin',$admin);
    }

    /**
     * 编辑管理员
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function postUpdate(Request $request){
        $this->validate($request, [
            'id' => 'required|integer',
            'username' => 'required|max:32|unique:admins,username,'.intval($request->input('id')).',id',
            'email' => 'required|email|unique:admins,email,'.intval($request->input('id')).',id',
            'old_password' => 'min:6'
        ]);
        $username = trim($request->input('username'));
        $oldPassword = $request->input('old_password');
        $email = $request->input('email');
        $id = intval($request->input('id'));
        $admin = User::find($id);
        if(empty($oldPassword)){
            $admin->username = $username;
            $admin->email = $email;
            $admin->updated_at = date('Y-m-d H:i:s',time());
            $admin->save();
            return redirect('backstage/administrators/list')->with('success_message', '修改成功');
        }
        $password = empty($admin)?'':$admin->password;
        if(!Hash::check($oldPassword, $password)){
            return redirect()->back()->with('error_message', '密码输入错误');
        }
        $this->validate($request, [
            'new_password' => 'required|min:6',
            'repet_password' => 'required|same:new_password'
        ]);
        $new_password = trim($request->input('new_password'));
        $admin->username = $username;
        $admin->email = $email;
        $admin->updated_at = date('Y-m-d H:i:s',time());
        $admin->password = Hash::make($new_password);
        $admin->save();
        return redirect('backstage/administrators/list')
            ->with('success_message', '修改成功');
    }

    /**
     * 添加角色页面
     * @param $id
     * @return mixed
     */
    public function getAddRole($id){
        $roles = Role::all();
        $role_select_ids = DB::table('role_user')->where('user_id',intval($id))->pluck('role_id')->toArray();
        foreach($roles as $role){
            $role->selected = in_array($role->id,$role_select_ids) ? 1 : 0;
        }
        return view('backstage.admin.add_role')
            ->with('roles',$roles)
            ->with('id',intval($id));
    }

    /**
     * 添加角色
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function postAddRole(Request $request){
        $role_ids = $request->input('role_ids');
        $role_select_ids = DB::table('role_user')->where('user_id',$request->input('id'))->pluck('role_id')->toArray();
        $admin = User::find(intval($request->input('id')));
        $admin->detachRoles(array(array('id'=>$role_select_ids)));
        $admin->attachRole(array('id'=>$role_ids));
        return redirect()->back()->with('success_message','添加成功');
    }
}
